<template>
  <div class="goods">
    <div class="goods-head">
      <div class="tabs">
        <van-tabs v-model="activeId" @click="navClick">
          <van-tab
            :title="item.name"
            :name="item.id"
            v-for="item in navList"
            :key="item.id"
          ></van-tab>
        </van-tabs>
      </div>
    </div>
    <div class="main">
      <div class="list-head">
        <h2>{{ goodsInfo.name }}</h2>
        <p>{{ goodsInfo.front_desc }}</p>
      </div>
      <ul class="list">
        <li
          class="item"
          v-for="item in goodsList"
          :key="item.id"
          @click="itemClick(item)"
        >
          <img :src="item.list_pic_url" alt="" />
          <p class="name">{{ item.name }}</p>
          <p class="price">￥{{ item.retail_price }}</p>
        </li>
      </ul>
    </div>
  </div>
</template>
<script>
import { categoryNav, goodsList } from "@/api/category/goods/index";
export default {
  data() {
    return {
      activeId: 0,
      navList: [],
      goodsInfo: {},
      goodsList: [],
    };
  },
  methods: {
    navClick(name) {
      console.log("NavClick:", name);
      this.activeId = name;
      this.getGoodsList();
    },
    getGoodsList() {
      goodsList({
        categoryId: this.activeId,
      }).then((res) => {
        console.log(res);
        this.goodsInfo = res.data.currentNav;
        this.goodsList = res.data.data;
      });
    },
    itemClick(item) {
      this.$router.push({
        path: "/goodsdetail",
        query: {
          id: item.id,
        },
      });
    },
  },
  created() {
    // 从路由中获取id
    let id = this.$route.query.id;
    this.activeId = Number(this.$route.query.id);
    console.log("id:", id);
    categoryNav({
      id,
    }).then((res) => {
      this.navList = res.data.navData;
    });

    this.getGoodsList();
  },
};
</script>
<style lang="scss" scoped>
@import "./index.scss";
</style>
